home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / x2p / find2perl < prev    next >
Encoding:
Text File  |  1996-07-07  |  12.0 KB  |  576 lines  |  [TEXT/MPS ]

  1. Perl -Sx "{0}" {"Parameters"}; Exit {Status}
  2. #!perl
  3.     eval 'exec perl -S $0 "$@"'
  4.     if 0;
  5. $startperl = 'Perl -Sx "{0}" {"Parameters"}; Exit {Status}
  6. #!perl';
  7. # Modified September 26, 1993 to provide proper handling of years after 1999
  8. #   Tom Link <tml+@pitt.edu>
  9. #   University of Pittsburgh
  10.  
  11. while ($ARGV[0] =~ /^[^-!(]/) {
  12.     push(@roots, shift);
  13. }
  14. if ($^O eq "MacOS") {
  15.     @roots = (':') unless @roots;
  16. } else {
  17.     @roots = ('.') unless @roots;
  18. }
  19. for (@roots) { $_ = "e($_); }
  20. $roots = join(',', @roots);
  21.  
  22. $indent = 1;
  23.  
  24. while (@ARGV) {
  25.     $_ = shift;
  26.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  27.     if ($_ eq '(') {
  28.     $out .= &tab . "(\n";
  29.     $indent++;
  30.     next;
  31.     }
  32.     elsif ($_ eq ')') {
  33.     $indent--;
  34.     $out .= &tab . ")";
  35.     }
  36.     elsif ($_ eq '!') {
  37.     $out .= &tab . "!";
  38.     next;
  39.     }
  40.     elsif ($_ eq 'name') {
  41.     $out .= &tab;
  42.     $pat = &fileglob_to_re(shift);
  43.     $out .= '/' . $pat . "/";
  44.     }
  45.     elsif ($_ eq 'perm') {
  46.     $onum = shift;
  47.     die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
  48.     if ($onum =~ s/^-//) {
  49.         $onum = '0' . sprintf("%o", oct($onum) & 017777);    # s/b 07777 ?
  50.         $out .= &tab . "((\$mode & $onum) == $onum)";
  51.     }
  52.     else {
  53.         $onum = '0' . $onum unless $onum =~ /^0/;
  54.         $out .= &tab . "((\$mode & 0777) == $onum)";
  55.     }
  56.     }
  57.     elsif ($_ eq 'type') {
  58.     ($filetest = shift) =~ tr/s/S/;
  59.     $out .= &tab . "-$filetest _";
  60.     }
  61.     elsif ($_ eq 'print') {
  62.     $out .= &tab . 'print("$name\n")';
  63.     }
  64.     elsif ($_ eq 'print0') {
  65.     $out .= &tab . 'print("$name\0")';
  66.     }
  67.     elsif ($_ eq 'fstype') {
  68.     $out .= &tab;
  69.     $type = shift;
  70.     if ($type eq 'nfs')
  71.         { $out .= '($dev < 0)'; }
  72.     else
  73.         { $out .= '($dev >= 0)'; }
  74.     }
  75.     elsif ($_ eq 'user') {
  76.     $uname = shift;
  77.     $out .= &tab . "(\$uid == \$uid{'$uname'})";
  78.     $inituser++;
  79.     }
  80.     elsif ($_ eq 'group') {
  81.     $gname = shift;
  82.     $out .= &tab . "(\$gid == \$gid{'$gname'})";
  83.     $initgroup++;
  84.     }
  85.     elsif ($_ eq 'nouser') {
  86.     $out .= &tab . '!defined $uid{$uid}';
  87.     $inituser++;
  88.     }
  89.     elsif ($_ eq 'nogroup') {
  90.     $out .= &tab . '!defined $gid{$gid}';
  91.     $initgroup++;
  92.     }
  93.     elsif ($_ eq 'links') {
  94.     $out .= &tab . '($nlink ' . &n(shift);
  95.     }
  96.     elsif ($_ eq 'inum') {
  97.     $out .= &tab . '($ino ' . &n(shift);
  98.     }
  99.     elsif ($_ eq 'size') {
  100.     $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
  101.     }
  102.     elsif ($_ eq 'atime') {
  103.     $out .= &tab . '(int(-A _) ' . &n(shift);
  104.     }
  105.     elsif ($_ eq 'mtime') {
  106.     $out .= &tab . '(int(-M _) ' . &n(shift);
  107.     }
  108.     elsif ($_ eq 'ctime') {
  109.     $out .= &tab . '(int(-C _) ' . &n(shift);
  110.     }
  111.     elsif ($_ eq 'exec') {
  112.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  113.     shift;
  114.     $_ = "@cmd";
  115.     if (m#^(/bin/)?rm -f {}$#) {
  116.         if (!@ARGV) {
  117.         $out .= &tab . 'unlink($_)';
  118.         }
  119.         else {
  120.         $out .= &tab . '(unlink($_) || 1)';
  121.         }
  122.     }
  123.     elsif (m#^(/bin/)?rm {}$#) {
  124.         $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
  125.     }
  126.     else {
  127.         for (@cmd) { s/'/\\'/g; }
  128.         $" = "','";
  129.         $out .= &tab . "&exec(0, '@cmd')";
  130.         $" = ' ';
  131.         $initexec++;
  132.     }
  133.     }
  134.     elsif ($_ eq 'ok') {
  135.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  136.     shift;
  137.     for (@cmd) { s/'/\\'/g; }
  138.     $" = "','";
  139.     $out .= &tab . "&exec(1, '@cmd')";
  140.     $" = ' ';
  141.     $initexec++;
  142.     }
  143.     elsif ($_ eq 'prune') {
  144.     $out .= &tab . '($prune = 1)';
  145.     }
  146.     elsif ($_ eq 'xdev') {
  147.     $out .= &tab . '!($prune |= ($dev != $topdev))';
  148.     }
  149.     elsif ($_ eq 'newer') {
  150.     $out .= &tab;
  151.     $file = shift;
  152.     $newername = 'AGE_OF' . $file;
  153.     $newername =~ s/[^\w]/_/g;
  154.     $newername = '$' . $newername;
  155.     $out .= "(-M _ < $newername)";
  156.     $initnewer .= "$newername = -M " . "e($file) . ";\n";
  157.     }
  158.     elsif ($_ eq 'eval') {
  159.     $prog = "e(shift);
  160.     $out .= &tab . "eval $prog";
  161.     }
  162.     elsif ($_ eq 'depth') {
  163.     $depth++;
  164.     next;
  165.     }
  166.     elsif ($_ eq 'ls') {
  167.     $out .= &tab . "&ls";
  168.     $initls++;
  169.     }
  170.     elsif ($_ eq 'tar') {
  171.     $out .= &tab;
  172.     die "-tar must have a filename argument\n" unless @ARGV;
  173.     $file = shift;
  174.     $fh = 'FH' . $file;
  175.     $fh =~ s/[^\w]/_/g;
  176.     $out .= "&tar($fh)";
  177.     $file = '>' . $file;
  178.     $initfile .= "open($fh, " . "e($file) .
  179.       qq{) || die "Can't open $fh: \$!\\n";\n};
  180.     $inittar++;
  181.     $flushall = "\n&tflushall;\n";
  182.     }
  183.     elsif (/^n?cpio$/) {
  184.     $depth++;
  185.     $out .= &tab;
  186.     die "-$_ must have a filename argument\n" unless @ARGV;
  187.     $file = shift;
  188.     $fh = 'FH' . $file;
  189.     $fh =~ s/[^\w]/_/g;
  190.     $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
  191.     $file = '>' . $file;
  192.     $initfile .= "open($fh, " . "e($file) .
  193.       qq{) || die "Can't open $fh: \$!\\n";\n};
  194.     $initcpio++;
  195.     $flushall = "\n&flushall;\n";
  196.     }
  197.     else {
  198.     die "Unrecognized switch: -$_\n";
  199.     }
  200.     if (@ARGV) {
  201.     if ($ARGV[0] eq '-o') {
  202.         { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
  203.         $statdone = 0 if $indent == 1 && $delayedstat;
  204.         $saw_or++;
  205.         shift;
  206.     }
  207.     else {
  208.         $out .= " &&" unless $ARGV[0] eq ')';
  209.         $out .= "\n";
  210.         shift if $ARGV[0] eq '-a';
  211.     }
  212.     }
  213. }
  214.  
  215. print <<"END";
  216. $startperl
  217.  
  218. eval 'exec perl -S \$0 \${1+"\$@"}'
  219.     if \$running_under_some_shell;
  220.  
  221. END
  222.  
  223. if ($initls) {
  224.     print <<'END';
  225. @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  226. @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  227.  
  228. END
  229. }
  230.  
  231. if ($inituser || $initls) {
  232.     print 'while (($name, $pw, $uid) = getpwent) {', "\n";
  233.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
  234.     print '    $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
  235.     print "}\n\n";
  236. }
  237.  
  238. if ($initgroup || $initls) {
  239.     print 'while (($name, $pw, $gid) = getgrent) {', "\n";
  240.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
  241.     print '    $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
  242.     print "}\n\n";
  243. }
  244.  
  245. print $initnewer, "\n" if $initnewer;
  246.  
  247. print $initfile, "\n" if $initfile;
  248.  
  249. $find = $depth ? "finddepth" : "find";
  250. print <<"END";
  251. require "$find.pl";
  252.  
  253. # Traverse desired filesystems
  254.  
  255. &$find($roots);
  256. $flushall
  257. exit;
  258.  
  259. sub wanted {
  260. $out;
  261. }
  262.  
  263. END
  264.  
  265. if ($initexec) {
  266.     print <<'END';
  267. sub exec {
  268.     local($ok, @cmd) = @_;
  269.     foreach $word (@cmd) {
  270.     $word =~ s#{}#$name#g;
  271.     }
  272.     if ($ok) {
  273.     local($old) = select(STDOUT);
  274.     $| = 1;
  275.     print "@cmd";
  276.     select($old);
  277.     return 0 unless <STDIN> =~ /^y/;
  278.     }
  279.     chdir $cwd;        # sigh
  280.     system @cmd;
  281.     chdir $dir;
  282.     return !$?;
  283. }
  284.  
  285. END
  286. }
  287.  
  288. if ($initls) {
  289.     print <<'END';
  290. sub ls {
  291.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  292.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  293.  
  294.     $pname = $name;
  295.  
  296.     if (defined $blocks) {
  297.     $blocks = int(($blocks + 1) / 2);
  298.     }
  299.     else {
  300.     $blocks = int(($size + 1023) / 1024);
  301.     }
  302.  
  303.     if    (-f _) { $perms = '-'; }
  304.     elsif (-d _) { $perms = 'd'; }
  305.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  306.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  307.     elsif (-p _) { $perms = 'p'; }
  308.     elsif (-S _) { $perms = 's'; }
  309.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  310.  
  311.     $tmpmode = $mode;
  312.     $tmp = $rwx[$tmpmode & 7];
  313.     $tmpmode >>= 3;
  314.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  315.     $tmpmode >>= 3;
  316.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  317.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  318.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  319.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  320.     $perms .= $tmp;
  321.  
  322.     $user = $user{$uid} || $uid;
  323.     $group = $group{$gid} || $gid;
  324.  
  325.     ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  326.     $moname = $moname[$mon];
  327.     if (-M _ > 365.25 / 2) {
  328.     $timeyear = $year + 1900;
  329.     }
  330.     else {
  331.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  332.     }
  333.  
  334.     printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  335.         $ino,
  336.          $blocks,
  337.               $perms,
  338.                 $nlink,
  339.                 $user,
  340.                      $group,
  341.                       $sizemm,
  342.                           $moname,
  343.                          $mday,
  344.                              $timeyear,
  345.                              $pname;
  346.     1;
  347. }
  348.  
  349. sub sizemm {
  350.     sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
  351. }
  352.  
  353. END
  354. }
  355.  
  356. if ($initcpio) {
  357. print <<'END';
  358. sub cpio {
  359.     local($nc,$fh) = @_;
  360.     local($text);
  361.  
  362.     if ($name eq 'TRAILER!!!') {
  363.     $text = '';
  364.     $size = 0;
  365.     }
  366.     else {
  367.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  368.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  369.     if (-f _) {
  370.         open(IN, "./$_\0") || do {
  371.         warn "Couldn't open $name: $!\n";
  372.         return;
  373.         };
  374.     }
  375.     else {
  376.         $text = readlink($_);
  377.         $size = 0 unless defined $text;
  378.     }
  379.     }
  380.  
  381.     ($nm = $name) =~ s#^\./##;
  382.     $nc{$fh} = $nc;
  383.     if ($nc eq 'n') {
  384.     $cpout{$fh} .=
  385.       sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  386.         070707,
  387.         $dev & 0777777,
  388.         $ino & 0777777,
  389.         $mode & 0777777,
  390.         $uid & 0777777,
  391.         $gid & 0777777,
  392.         $nlink & 0777777,
  393.         $rdev & 0177777,
  394.         $mtime,
  395.         length($nm)+1,
  396.         $size,
  397.         $nm);
  398.     }
  399.     else {
  400.     $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
  401.     $cpout{$fh} .= pack("SSSSSSSSLSLa*",
  402.         070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
  403.         length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
  404.     }
  405.     if ($text ne '') {
  406.     $cpout{$fh} .= $text;
  407.     }
  408.     elsif ($size) {
  409.     &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
  410.     while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  411.         &flush($fh);
  412.         $l = length($cpout{$fh});
  413.     }
  414.     }
  415.     close IN;
  416. }
  417.  
  418. sub flush {
  419.     local($fh) = @_;
  420.  
  421.     while (length($cpout{$fh}) >= 5120) {
  422.     syswrite($fh,$cpout{$fh},5120);
  423.     ++$blocks{$fh};
  424.     substr($cpout{$fh}, 0, 5120) = '';
  425.     }
  426. }
  427.  
  428. sub flushall {
  429.     $name = 'TRAILER!!!';
  430.     foreach $fh (keys %cpout) {
  431.     &cpio($nc{$fh},$fh);
  432.     $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  433.     &flush($fh);
  434.     print $blocks{$fh} * 10, " blocks\n";
  435.     }
  436. }
  437.  
  438. END
  439. }
  440.  
  441. if ($inittar) {
  442. print <<'END';
  443. sub tar {
  444.     local($fh) = @_;
  445.     local($linkname,$header,$l,$slop);
  446.     local($linkflag) = "\0";
  447.  
  448.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  449.       $atime,$mtime,$ctime,$blksize,$blocks) = lstat(_);
  450.     $nm = $name;
  451.     if ($nlink > 1) {
  452.     if ($linkname = $linkseen{$fh,$dev,$ino}) {
  453.         $linkflag = 1;
  454.     }
  455.     else {
  456.         $linkseen{$fh,$dev,$ino} = $nm;
  457.     }
  458.     }
  459.     if (-f _) {
  460.     open(IN, "./$_\0") || do {
  461.         warn "Couldn't open $name: $!\n";
  462.         return;
  463.     };
  464.     $size = 0 if $linkflag ne "\0";
  465.     }
  466.     else {
  467.     $linkname = readlink($_);
  468.     $linkflag = 2 if defined $linkname;
  469.     $nm .= '/' if -d _;
  470.     $size = 0;
  471.     }
  472.  
  473.     $header = pack("a100a8a8a8a12a12a8a1a100",
  474.     $nm,
  475.     sprintf("%6o ", $mode & 0777),
  476.     sprintf("%6o ", $uid & 0777777),
  477.     sprintf("%6o ", $gid & 0777777),
  478.     sprintf("%11o ", $size),
  479.     sprintf("%11o ", $mtime),
  480.     "        ",
  481.     $linkflag,
  482.     $linkname);
  483.     $l = length($header) % 512;
  484.     substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
  485.     substr($header, 154, 1) = "\0";  # blech
  486.     $tarout{$fh} .= $header;
  487.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  488.     if ($size) {
  489.     &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
  490.     while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  491.         $slop = length($tarout{$fh}) % 512;
  492.         $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  493.         &tflush($fh);
  494.         $l = length($tarout{$fh});
  495.     }
  496.     }
  497.     close IN;
  498. }
  499.  
  500. sub tflush {
  501.     local($fh) = @_;
  502.  
  503.     while (length($tarout{$fh}) >= 10240) {
  504.     syswrite($fh,$tarout{$fh},10240);
  505.     ++$blocks{$fh};
  506.     substr($tarout{$fh}, 0, 10240) = '';
  507.     }
  508. }
  509.  
  510. sub tflushall {
  511.     local($len);
  512.  
  513.     foreach $fh (keys %tarout) {
  514.     $len = 10240 - length($tarout{$fh});
  515.     $len += 10240 if $len < 1024;
  516.     $tarout{$fh} .= "\0" x $len;
  517.     &tflush($fh);
  518.     }
  519. }
  520.  
  521. END
  522. }
  523.  
  524. exit;
  525.  
  526. ############################################################################
  527.  
  528. sub tab {
  529.     local($tabstring);
  530.  
  531.     $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
  532.     if (!$statdone) {
  533.     if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
  534.         $delayedstat++;
  535.     }
  536.     else {
  537.         if ($saw_or) {
  538.         $tabstring .= <<'ENDOFSTAT' . $tabstring;
  539. ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  540. ENDOFSTAT
  541.         }
  542.         else {
  543.         $tabstring .= <<'ENDOFSTAT' . $tabstring;
  544. (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  545. ENDOFSTAT
  546.         }
  547.         $statdone = 1;
  548.     }
  549.     }
  550.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  551.     $tabstring;
  552. }
  553.  
  554. sub fileglob_to_re {
  555.     local($tmp) = @_;
  556.  
  557.     $tmp =~ s#([./^\$()])#\\$1#g;
  558.     $tmp =~ s/([?*])/.$1/g;
  559.     "^$tmp\$";
  560. }
  561.  
  562. sub n {
  563.     local($n) = @_;
  564.  
  565.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  566.     $n =~ s/ 0*(\d)/ $1/;
  567.     $n . ')';
  568. }
  569.  
  570. sub quote {
  571.     local($string) = @_;
  572.     $string =~ s/'/\\'/;
  573.     "'$string'";
  574. }
  575.